package com.bbcare.comm.ExcelUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


/**
 * Created by ZCL on 2019/2/21.
 */
public class ReadExcelUtil {
    public static List ProcessingList() {
        ReadExcelUtil obj = new ReadExcelUtil();
        // 此处为创建Excel路径
        String path = "E:/SZ产科队列.xlsx";
        List excelList = obj.readExcel(path);
        List handleList = new ArrayList();
        for (int i = 1; i < excelList.size(); i++) {
            List list = (List) excelList.get(i);
            Map listMap = new HashMap();
            for (int j = 0; j < list.size(); j++) {
                if (j == 0) {
                    listMap.put("Pub1100002", list.get(j));
                } else if (j == 1) {
                    listMap.put("341200007", list.get(j));
                } else if (j == 2) {
                    listMap.put("Pub1100001", list.get(j));
                } else if (j == 3) {
                    listMap.put("Pub12x00008", list.get(j));
                } else if (j == 4) {
                    String time = (String) list.get(j);
                    String[] times = time.split(" ");
                    time = times[0];
                    listMap.put("fzszckzd_cysj", time);
                } else if (j == 5) {
                    String time = (String) list.get(j);
                    String[] times = time.split(" ");
                    time = times[0];
                    listMap.put("Pub1100004", time);
                }
            }
            handleList.add(listMap);
        }
        return handleList;
    }

    public List readExcel(String path) {
        String fileType = path.substring(path.lastIndexOf(".") + 1);
        List lists = new ArrayList();
        //读取excel文件
        InputStream is = null;
        try {
            is = new FileInputStream(path);
            //获取工作薄
            Workbook wb = null;
            if (fileType.equals("xls")) {
                wb = new HSSFWorkbook(is);
            } else if (fileType.equals("xlsx")) {
                wb = new XSSFWorkbook(is);
            } else {
                return null;
            }

            //读取第一个工作页sheet
            Sheet sheet = wb.getSheetAt(0);
            //第一行为标题
            for (Row row : sheet) {
                ArrayList list = new ArrayList();
                for (Cell cell : row) {
                    //根据不同类型转化成字符串
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    list.add(cell.getStringCellValue());
                }
                lists.add(list);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return lists;
    }
}
